import mongoose from 'mongoose'
// 表结构对象
const accessTokenSchema = new mongoose.Schema(
  {
    accessToken: String,
    expireTime: Number,
  },
  {
    timestamps: {
      createdAt: 'createTime',
      updatedAt: 'updateTime',
    },
  }
)
// 自定义获取token静态方法
accessTokenSchema.statics.getAccessToken = async function () {
  return await this.findOne()
}
// 自定义存储token静态方法
accessTokenSchema.statics.setAccessToken = async function (accessToken) {
  // 有则更新，无则添加
  const query = {}
  const options = { upsert: true }
  return await this.updateOne(query, accessToken, options)
}
// 创建模型类并导出
module.exports = mongoose.model('AccessToken', accessTokenSchema)
